package com.sfy.designdemo.policyandfactory.controller;

import com.sfy.designdemo.policyandfactory.context.PayContext;
import com.sfy.designdemo.policyandfactory.dto.OrderDTO;
import com.sfy.designdemo.policyandfactory.dto.PayResult;
import com.sfy.designdemo.policyandfactory.service.IPayment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by SiFeiYang on Date 2022/4/6
 */
@RestController
public class PayController {
    @Autowired
    PayContext payContext;

    /**
     * 策略模式+工厂模式
     * 根据定义的策略名称，从context中获取对应的bean，从而走向不同的策略
     * 工厂模式：我们明确地计划不同条件下创建不同实例时。
     * 策略模式：定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。
     * @param orderDTO
     * @return
     */
    @GetMapping("/pay")
    public PayResult pay(OrderDTO orderDTO){
        IPayment iPayment = payContext.getiPaymentByType(orderDTO.getPaymentType());
        return iPayment.pay(orderDTO);
    }

}
